﻿' ------------------------------------------------------------------------------
' This form hosts the WpfImageViewer control that displays a WPF BitmapImage
' for a photo. It loads the image from the filestream for a given row.
' ------------------------------------------------------------------------------
' This file is part of the SQL Server 2008 Technology Demonstration. This
' application was designed and coded by Eric Weitzman, LandTime Inc, for
' Microsoft Corporation. Contact Ed Katibah at Microsoft about this demo.
' Copyright © 2008 Microsoft Corporation. All rights reserved.
' ------------------------------------------------------------------------------

Imports System.Windows.Media.Imaging

Partial Public Class ImageViewerForm

    Private m_scale As Double = 0
    Private m_location As Point
    Private m_imagesize As Point
    Private m_image_to_load As BitmapImage

    Private Sub ImageViewer_Loaded(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Loaded
    End Sub

    Public Shared Sub ShowFullImage(ByVal img As BitmapImage, ByVal caption As String)
        Dim iv As New ImageViewerForm
        iv.SetCaption(caption)
        iv.SetImage(img)
        iv.LoadImageOnLoadedEvent()
        iv.ShowDialog()
    End Sub

    Public Shared Sub ShowFullImage(ByVal row As DataRow)
        If row Is Nothing Then Return
        Dim rowguid As Guid = CType(row.Item("RowGuid"), Guid)
        If rowguid = Guid.Empty Then Return
        Trace.WriteLine("displaying image for row: " & rowguid.ToString())
        Dim caption As String = "Image read from FILESTREAM - original filename was '" & row.Item("FileName").ToString & "'"
        Dim image As BitmapImage = SpatialAlbum.Core.ImageUtils.LoadImageFromFilestream( _
                                        rowguid, My.Settings.TABLE_Photo, My.Settings.COLUMN_ImageFilestream, g_connection)
        ShowFullImage(image, caption)
    End Sub

    Public Sub SetCaption(ByVal caption As String)
        Dim sb As New System.Text.StringBuilder
        sb.Append(caption)
        sb.Append(" (size=").Append(m_imagesize.X).Append("x").Append(m_imagesize.Y)
        sb.Append(" mag=").Append(CInt(Math.Round(m_scale * 100, 0))).Append("%)")
        sb.Append(" [double-click on image to close window]")
        Me.Title = sb.ToString
    End Sub

    Public Sub SetImage(ByVal image As BitmapImage)
        m_image_to_load = image
    End Sub

    Private Sub LoadImageOnLoadedEvent()
        Image1.Source = m_image_to_load
        Dim w As Integer = m_image_to_load.PixelWidth
        Dim h As Integer = m_image_to_load.PixelHeight
        m_imagesize = New Point(w, h)
        Dim sw As Integer = My.Computer.Screen.Bounds.Width
        Dim sh As Integer = My.Computer.Screen.Bounds.Height

        Dim newsize As SpatialAlbum.Core.Size = SpatialAlbum.Core.ImageUtils.ShrinkAspect(w, h, sw, sh, 0.9)
        m_scale = newsize.Width / w
        Me.Width = newsize.Width
        Me.Height = newsize.Height
        m_location = New Point(CInt((sw - Me.Width) / 2), CInt((sh - Me.Height) / 2))
        Me.Left = m_location.X
        Me.Top = m_location.Y
    End Sub

    Private Sub ImageViewer_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.MouseDoubleClick
        Me.Close()
    End Sub

    Private Sub ImageViewerForm_StateChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.StateChanged
        Static ignore_statechanged As Boolean = False
        If Me.WindowState <> Windows.WindowState.Maximized Then Return
        If ignore_statechanged Then Return

        ignore_statechanged = True

        Me.Visibility = Windows.Visibility.Hidden

        Me.WindowState = Windows.WindowState.Normal
        Me.Width = My.Computer.Screen.Bounds.Width
        Me.Height = My.Computer.Screen.Bounds.Height
        Me.Left = 0
        Me.Top = 0

        Viewbox1.Width = Me.Width
        Viewbox1.Height = Me.Height
        'Image1.InvalidateMeasure()
        Me.Visibility = Windows.Visibility.Visible

        ignore_statechanged = False
    End Sub

    Private Sub Viewbox1_SizeChanged(ByVal sender As Object, ByVal e As System.Windows.SizeChangedEventArgs) Handles Viewbox1.SizeChanged

    End Sub
End Class
